﻿using Microsoft.EntityFrameworkCore;
using MyMes.Infrastructure;
using MyMES.IServices.ProductInspectionTemplates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.Core;

namespace MyMES.Services.ProductInspectionTemplates
{
    public class ProductInspectionTemplateServices : IProductInspectionTemplateServices
    {
        private readonly IBaseRepository<ProductInspectionTemplate> _productinspectiontemplateRepository;

        public ProductInspectionTemplateServices(IBaseRepository<ProductInspectionTemplate> productinspectiontemplateRepository)
        {
            _productinspectiontemplateRepository = productinspectiontemplateRepository;
        }

        public async Task<WebResponseContent> AddProductInspectionTemplateModel(ProductInspectionTemplate productInspectionTemplate)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _productinspectiontemplateRepository.AddAsync(productInspectionTemplate);
                return response.OK("添加产品成功");
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<WebResponseContent> DelProductInspectionTemplateModel(ProductInspectionTemplate productInspectionTemplate)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {

                await _productinspectiontemplateRepository.Remove(productInspectionTemplate);
                return response.OK("删除产品成功");
            }
            catch (Exception ex)
            {
                return response.Error($"删除产品失败：{ex.Message}");
            }
        }

        public async Task<PageGridData<ProductInspectionTemplate>> GetProductInspectionTemplate(string? PlanNo, int pageIndex = 1, int pageSize = 10)
        {
            var pageData = new PageGridData<ProductInspectionTemplate>();
            var query = _productinspectiontemplateRepository.GetAll(x => x.PlanNo == PlanNo);

            pageData.total = query.Count();

            pageData.rows = await query.OrderByDescending(x => x.Id)
                             .Skip((pageIndex - 1) * pageSize).Take(pageSize)
                                  .ToListAsync();
            return pageData;
        }

        public async Task<WebResponseContent> UpdateProductInspectionTemplateModel(ProductInspectionTemplate productInspectionTemplate)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _productinspectiontemplateRepository.UpdateAsync(productInspectionTemplate);
                return response.OK("更新产品成功");
            }
            catch (Exception ex)
            {
                return response.Error($"更新产品失败：{ex.Message}");
            }
        }
    }
}
